<template>
  <div>
    <h1>Login</h1>
    <input type="text" v-model="username">
    <input type="text" v-model="password">
    <button @click="login">登录</button>

  </div>
</template>

<script>
  import {setToken} from "../utils/storage";

  export default {
    data() {
      return {
        username: '',
        password: ''
      }
    },

    methods: {
      login() { // dispatch : 调用actions
        this.$store.dispatch('users/login',
          {username: this.username, password: this.password})
          .then(res => {
            if (res.code === 1) {
              setToken(res.token);
              this.$router.push('/main');
            } else {
              // element-ui
              this.$message.error('登录失败');
            }
          })
      }
    },
    name: "Login"
  }
</script>

<style scoped>

</style>
